package com.googlecode.continuity.sample.basic.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.InvocationException;
import com.google.gwt.user.client.ui.*;
import com.googlecode.continuity.core.client.CoreGWT;
import com.googlecode.continuity.plugin.codecomment.client.CodeCommentScreen;
import com.googlecode.continuity.plugin.codecomment.client.CodePointer;
import com.googlecode.continuity.plugin.gwtext.client.ErrorBox;
import com.googlecode.continuity.plugin.gwtext.client.LoaderBox;
import com.googlecode.continuity.plugin.svnbrowser.client.*;
import com.googlecode.continuity.plugin.svnbrowser.client.wrapper.SVNDirEntryWrapper;

/**
 * TestPage
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class TestPagePreselected implements EntryPoint {

  private int fileRequestCounter = 0;

  public void onModuleLoad() {
    Button browse = new Button("Browse", new ClickListener() {
      public void onClick(Widget widget) {
        final String repositoryURL =
            "http://continuity.googlecode.com/svn/trunk/src/com/googlecode/continuity/sample/basic/sampleproject/"; // TODO
        final ReadSVNRPCAsync svnRPCAsync = (ReadSVNRPCAsync) CoreGWT.getRPC("ReadSVNRPCAsync");

        // retrieve repository connection
        svnRPCAsync.connect(repositoryURL, "", "", new AsyncCallback() {
          public void onFailure(Throwable throwable) {
            ErrorBox box = new ErrorBox(throwable);
            box.show();
          }

          // show file browser
          public void onSuccess(Object object) {
            final SVNRepositoryHandle handle = (SVNRepositoryHandle) object;
            SVNBrowser svnBrowser = new SVNBrowser(handle);
            svnBrowser.setRootPrefix("Sample Project");
            svnBrowser.show();

            // listen for file browser selections
            svnBrowser.addSVNBrowserEventListener(new SVNBrowserEventListener() {
              public void onDirectorySelected(String directoryPath) {
              }

              // retrieve file contents
              public void onFileSelected(SVNDirEntryWrapper entry, final String filePath) {
                final CodePointer pointer = new CodePointer();
                pointer.setUrl(entry.getURL());
                pointer.setName(entry.getName());
                pointer.setFirstLine(1);
                pointer.setVersion(entry.getRevision());

                //todo: show loader
                final PopupPanel loader = new LoaderBox("Loading file contents...");
                loader.show();

                final int requestId = ++fileRequestCounter;

                // show file contents
                svnRPCAsync.getTextFileContents(handle, filePath, pointer.getVersion(),
                    new AsyncCallback() {
                      public void onFailure(Throwable throwable) {
                        if (!loader.isVisible() || requestId != fileRequestCounter) {
                          return;
                        }
                        loader.hide();
                        String error = "";
                        try {
                          throw throwable;
                        } catch (InvocationException e) {
                          error = e.getMessage();
                        } catch (ReadSVNException e) {
                          error = e.getMessage();
                        } catch (Throwable t) {
                          GWT.getUncaughtExceptionHandler().onUncaughtException(t);
                        }
                        ErrorBox box = new ErrorBox(error);
                        box.show();
                      }

                      public void onSuccess(Object object) {
                        if (!loader.isVisible() || requestId != fileRequestCounter) {
                          return;
                        }
                        String code = (String) object;
                        CodeCommentScreen box = new CodeCommentScreen(handle, pointer, code);
                        loader.hide();
                        box.show();
                      }
                    });
              }
            });
          }
        });
      }
    });

    DOM.setInnerHTML(RootPanel.get("browseContainer").getElement(), "");
    RootPanel.get("browseContainer").add(browse);
  }
}